/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EthernetTriggerConfigurationPanel.java
 *
 * Created on 2012-01-25, 09:35:27
 */

package Visual.Triggers;

import Tools.CharsetControl;
import Tools.ConfigurationManager;
import Tools.ToolBox;
import Visual.Tools.DataArea;
import Visual.Tools.DocumentAdapter;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 *
 * @author pawelkn
 */
public class EthernetTriggerConfigurationPanel extends javax.swing.JPanel {
    private static ResourceBundle bundle;

    private DataArea dataArea;

    /** Creates new form EthernetTriggerConfigurationPanel */
    public EthernetTriggerConfigurationPanel() {
        Locale locale = new Locale(ConfigurationManager.load().getLocale());
        ResourceBundle.clearCache();
        bundle = ResourceBundle.getBundle("Bundle", locale, new CharsetControl());        
        
        initComponents();

        dataArea = new DataArea();
        dataArea.setDataState(DataArea.DATASTATE_ASCI);
        jScrollPane.setViewportView(dataArea);

        portTextField.getDocument().addDocumentListener( new DocumentAdapter() {
            @Override
            public void update() {
                ToolBox.tryParseIntPositive(portTextField);
            }
        });
    }

    public byte[] getCommand() {
        return dataArea.getData();
    }

    public void setCommand(byte[] command) {
        dataArea.setData(command);
    }

    public String getHost() {
        return hostTextField.getText();
    }

    public void setHost(String host) {
        hostTextField.setText(host);
    }

    public int getPort() {
        try {
            return Integer.parseInt(portTextField.getText());
        } catch(Exception e) {
            return 0;
        }
    }

    public void setPort(int port) {
        portTextField.setText(Integer.toString(port));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        hostTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        portTextField = new javax.swing.JTextField();
        jScrollPane = new javax.swing.JScrollPane();
        jLabel3 = new javax.swing.JLabel();
        commandModeComboBox = new javax.swing.JComboBox();

        jLabel1.setText(bundle.getString("HOST:")); // NOI18N

        jLabel2.setText(bundle.getString("PORT:")); // NOI18N

        jLabel3.setText(bundle.getString("SEND A COMMAND:")); // NOI18N

        commandModeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ASCI", "HEX" }));
        commandModeComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                commandModeComboBoxItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(portTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                            .addComponent(hostTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 208, Short.MAX_VALUE)
                        .addComponent(commandModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(hostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(commandModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void commandModeComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_commandModeComboBoxItemStateChanged
        if( "ASCI".equals( commandModeComboBox.getSelectedItem().toString() ) ) {
            dataArea.setDataState(DataArea.DATASTATE_ASCI);
        } else {
            dataArea.setDataState(DataArea.DATASTATE_HEX);
        }
}//GEN-LAST:event_commandModeComboBoxItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox commandModeComboBox;
    private javax.swing.JTextField hostTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTextField portTextField;
    // End of variables declaration//GEN-END:variables

}
